package Units;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.Timer;

import jgame.JGObject;
import jgame.JGPoint;
import jgame.JGTimer;
import Main.Board;
import Main.Game;
import Other.Field;

public abstract class Unit extends JGObject {
	public Game game = null;
	
	public Unit(String arg0, boolean arg1, double arg2, double arg3, int arg4,
			String arg5) {
		super(arg0, arg1, arg2, arg3, arg4, arg5);
		CID = arg4;
		steps = 1;
		alive = true;
		health = 1;
		moveSound = false;
	}
	
	private int health;
	private String name;
	private Field field;
	private Field target;
	private boolean side;	// false - attacks from bottom, otherwise true
	private int CID;
	private int state = 0;
	private int steps;
	boolean alive;
	public boolean moveSound;
	
	// abstract methods
	public abstract boolean canMove(Field field);
	public abstract boolean canAttack(Field field);
	public abstract ArrayList<Field> whereAttack(Field field);
	
	// get methods
	public int getHealth(){ return health; }
	public String getUnitName(){ return name; }
	public Field getField(){ return field; }
	public boolean getSide(){ return side; }
	public int getCID(){ return CID; }
	public int getState(){ return state; }
	public Field getTargetField(){ return target; }
	public int getSteps(){ return steps; }
	public boolean isAlive(){ return alive; }
	
	// set methods
	public void setHealth(int health){ this.health = health; }
	public void setUnitName(String name){ this.name = name; }
	public void setField(Field field){ this.field = field; }
	public void setSide(boolean side){ this.side = side; }
	public void setCID(int CID){ this.CID = CID; }
	public void setState(int state){ this.state = state; }
	public void setTargetField(Field f){ target = f; }
	public void setSteps(int steps){ this.steps = steps; }
	public void resetSteps(){ steps = 1; }
	public void decreaseSteps(){ steps--; }
	public void setAlive(boolean alive){ this.alive = alive; }
	
	public void kill(){
		setHealth(0);
		setAlive(false);
		suspend();
		remove();
		destroy();
	}
	
	// move methods
	public void goTo(Field target){
		moveSound = true;
		Game.board.increaseCurrentStep();
		Game.board.setAnimation(true);
		this.target = target;
		
		JGPoint p1 = Game.board.getRealCoordinates(this.getField());
		JGPoint p2 = Game.board.getRealCoordinates(target);
		
		Game.board.resetAllowed();
		decreaseSteps();
		
		setState(3);
		xspeed = (p2.x - p1.x) * 0.03;
		yspeed = (p2.y - p1.y) * 0.03;
		
		// timer
		new JGTimer(5, true){
			@Override
			public void alarm() {
				setState(2);
			}
		};
	}
	
	public static Unit getUnitFromCID(int cid){
		Board b = Game.board;
		
		for (int i = 0; i < 5; i++){
			if (b.robots1[i].getCID() == cid) return b.robots1[i];
			else if (b.dragons1[i].getCID() == cid) return b.dragons1[i];
		}
		
		for (int i = 0; i < 2; i++){
			if (b.robots2[i].getCID() == cid) return b.robots2[i];
			else if (b.dragons2[i].getCID() == cid) return b.dragons2[i];
		}
		
		for (int i = 0; i < 2; i++){
			if (b.robots3[i].getCID() == cid) return b.robots3[i];
			else if (b.dragons3[i].getCID() == cid) return b.dragons3[i];
		}
		
		for (int i = 0; i < 2; i++){
			if (b.robots4[i].getCID() == cid) return b.robots4[i];
			else if (b.dragons4[i].getCID() == cid) return b.dragons4[i];
		}
		
		if (b.robotKing.getCID() == cid) return b.robotKing;
		else if (b.dragonKing.getCID() == cid) return b.dragonKing;
		
		return null;
	}
}
